package cfdt.ldl.dspchannel.system;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import cfdt.common.utils.nacos.NacosConfigLocalCatch;
import cfdt.ldl.tools.exception.CodeException;
import cfdt.tools.util.StringUtils;

/**
 * 系统公用信息
 */
@Component
@RefreshScope
public class SystemEnvironment {

    public static final Logger logger = LoggerFactory.getLogger("SystemEnvironment");
    @Autowired
    private NacosConfigLocalCatch nacosConfigLocalCatch;
    public static String requestUrl;
    public static int connectTimeOut;
    public static int readTimeOut;

    @Value("${requestUrl}")
    public void setUrl(String requestUrl) {
        SystemEnvironment.requestUrl = requestUrl;
    }

    @Value("${connectTimeOut}")
    public void setConnectTimeOut(int connectTimeOut) {
        SystemEnvironment.connectTimeOut = connectTimeOut;
    }

    @Value("${readTimeOut}")
    public void setReadTimeOut(int readTimeOut) {
        SystemEnvironment.readTimeOut = readTimeOut;
    }

    public Map<String, String> getProperties(String institutionId) throws CodeException {
        @SuppressWarnings("unchecked")
        Map<String, Map<String, String>> dspMap = nacosConfigLocalCatch.get("dsp-busi.json", Map.class);
        logger.info(dspMap.toString());
        Map<String, String> map = null;
        if (StringUtils.isNotEmpty(institutionId)) {
            map = dspMap.get(institutionId);
            if (map.isEmpty()) {
                logger.info(String.valueOf("未找到配置信息[institutionId = " + institutionId));
                throw new CodeException("999999", "未找到配置信息,请联系开发人员");
            }
        }
        return map;
    }
}
